package com.ly.cloud.service.impl;

import com.ly.cloud.dto.UserGroupDto;
import com.ly.cloud.entity.UserGroupPo;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.UserGroupMapper;
import com.ly.cloud.service.UserGroupService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.UserGroupVo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: pengshiqng
 * @Description:
 * @Date: Create in 2018/12/6
 */
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public int insert(UserGroupDto dto,String username) {
        try {
			//把dto对象转换成po对象
			UserGroupPo po = BeanCopyUtils.copyBeanProperties(dto, UserGroupPo.class);
			po.setCjr(username);
			po.setCjsj(new Date());
			return userGroupMapper.insert(po);
		} catch (Exception e) {
			throw new DBException("新增用户组失败！", e);
		}
    }

	
	public int deleteById(Long id) {
		try {
			return userGroupMapper.deleteById(id);
		} catch (Exception e) {
			throw new DBException("删除用户组失败！", e);
		}
	}

	
	public int deleteByIds(List<Long> ids) {
		try {
			return userGroupMapper.deleteBatchIds(ids);
		} catch (Exception e) {
			throw new DBException("批量删除用户组失败！", e);
		}
	}

	
	public int updateById(UserGroupDto dto, String username) {
		try {
			//把dto对象转换成po对象
			UserGroupPo po = BeanCopyUtils.copyBeanProperties(dto, UserGroupPo.class);
			po.setXgr(username);
			po.setXgsj(new Date());
			return userGroupMapper.insert(po);
		} catch (Exception e) {
			throw new DBException("新增用户组失败！", e);
		}
	}

	public List<UserGroupVo> findByParentId(Long id) {
		try {
            return userGroupMapper.findByParentId(id);
		} catch (Exception e) {
			throw new DBException("根据父节点id查询用户组信息失败！", e);
		}
	}

    /**
     * @Description: 查询用户组编号是否存在
     * @Author: pengshiqing
     * @CreateDate: 2018/12/24
     */
    @Override
    public Boolean checkedZbh(Long zhb) {
        int result = userGroupMapper.checkedZbh(zhb);
        if (result == 0){
            return false;
        }
        return true;
    }
    
    
}
